<?php
/**
 * 《业务信息》管理类
 *
 * @author 孙晓晔
 * @version $Id: Role.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */

require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Service extends InfoUtil {
	public $tbl = Constant::tbl_service;

	public $orderby = array(
		'-' => '请选择',
		'emp' => '负责人',
		'code' => '编号',
		'name' => '名称',
		'category' => '分类',
			
		'1' => '创建时间',
		'2' => '更新时间',			
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:35:48 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$category = $this->_category();
		$controller->assign_by_ref('category_option', $category);
	}

	/**
	 * 查找
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/service/find');
		return true;
	}

	function _category() {
		$key = $this->tbl;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT DISTINCT category FROM %s WHERE length(category) > 0";
		$sql = sprintf($fmt, $this->tbl);
		debug($sql);
		$result = $db->query($sql);
		$ret = array('-'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$ret[$row['category']] = $row['category'];
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	/**
	 * 清除缓存
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_category($controller, $request) {
		$key = $this->tbl;
		clean($key);

		return true;
	}

	function _where($request) {
		$code_s = $this->_input($request['code_s']);
		
		$name_s = $request['name_s'];
		$category_s = $request['category_s'];

		$where = " WHERE 1=1";
		if (!empty($code_s)) {
			$where .= " AND code = '" . $code_s . "'";
		}

		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR content LIKE '%" . $name_s . "%')";
		}

		if (!empty($category_s) && $category_s <> '-') {
			$where .= " AND category = '" . $category_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 * 列表
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['num_'] = $this->_num($row['code']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/service/list');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	/**
	 * 获得权限信息
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function _num($service) {
		require_once(dirname(__FILE__) . '/../emp/EmpService.class.php');
		$aa = new EmpService();
		
		return $aa->_num($service);
	}

	/**
	 * 添加
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/service/add');
		return true;
	}

	/**
	 * 添加
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$code = $this->_input($request['code']);
		$name = $request['name'];

		$category = $request['category'];
		$content= $request['content'];
		$status = $request['status'];
			
		if (empty($name)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
			$this->findPost($controller, $request);
			return false;
		}

		clean($this->tbl . "_status_" . $status);

		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, code, name, zjm, category, content, status, createtime) 
				VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $code, $name, $zjm, $category, $content, $status);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 编辑
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		$controller->display($request, 'info/service/edit');
		return true;
	}

	/**
	 * 编辑修改
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
			
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$code = $this->_input($request['code']);
		$name = $request['name'];

		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);

		$category = $request['category'];
		$content= $request['content'];
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', code = '%s', name = '%s', zjm = '%s', category = '%s', 
				content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $code, $name, $zjm, $category, $content, $status, $id);
		debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
		$controller->display($request, 'info/service/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		if (!empty($id)) {
			$row = $this->_view($id);
		} else {
			$row = $this->code($this->tbl, $request['code']);
			
			$row = $this->_view(null, $row);
		}

		$controller->assign_by_ref('result', $row);
		$controller->display($request, 'info/service/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
	
		$row = $this->_view_t($row);
	
		return $row;
	}
	
	function emp($controller, $request) {
		$this->pretreat($controller);
			
		$id = $request['id'];
		$service = $this->_view($id);
	
		$controller->assign_by_ref('service', $service);
	
		require_once(dirname(__FILE__) . '/../emp/Emp.class.php');
		$aa = new Emp();
		
		$emp = $aa->_list();
		
		require_once(dirname(__FILE__) . '/../emp/EmpService.class.php');
		$bb = new EmpService();
		
		$data = array();
		$no = 1;
		while (list($k, $row) = each($emp)) {
			$row['no'] = $no++;
				
			if ($bb->_emp_service($row['id'], $service['code'])) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}
	
			$data_all[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);
	
		$controller->display($request, 'info/service/emp');
		return true;
	}
	
	/**
	 * 分配
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function empPost($controller, $request) {
		$id = $request['id'];
		$code = $request['code'];
	
		require_once(dirname(__FILE__) . '/../emp/EmpService.class.php');
		$aa = new EmpService();
	
		$aa->_reset($code);
	
		$list = $request['list'];
		if (empty($list)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
			$this->findPost($controller, $request);
			return true;
		}
	
		$bb = explode(',', $list);
		while (list($k, $emp) = each($bb)) {
			$aa->_update($emp, $code);
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		$insql = " in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "delete from " . $this->tbl . " WHERE id " . $insql;
		$query = $db->query($sql);

		$sql = "delete from " . Constant::tbl_role_priv . " where role " . $insql;
		// debug($sql);
		$query = $db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_zjm($controller, $request) {
		
		$data = $this->_ajax_zjm($request);
		echo json_encode($data);
	}

	function _ajax_zjm($request) {
		$q = $request['q'];
		
		$key = "service.ajax_zjm_" . $q;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE zjm LIKE '%s'";
		$sql = sprintf($fmt, $this->tbl, $q . "%");
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			// $row['dept_t'] = Name::dept($row['dept']);

			$ret[] = $row;
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}
	
	function ajax_code($controller, $request) {
		
		$data = $this->_ajax_code($request);
		echo json_encode($data);
	}

	function _ajax_code($request) {
		$q = $request['q'];
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code LIKE '%s' ORDER BY code";
		$sql = sprintf($fmt, $this->tbl, $q . "%");
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$ret[] = $row;
		}

		return $ret;
	}

	function u($controller, $request) {
		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['name']);
			$zjm = strtoupper($zjm);

			$fmt = "UPDATE %s SET zjm = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $zjm, $row['id']);
			debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
}
?>